package com.ui.basic.service.masterdata;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.jsecurity.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.common.basic.util.ComboBoxPopulator;
import com.common.basic.util.Globals;
import com.ui.basic.assembler.masterdata.CurrencyAssembler;
import com.ui.basic.command.masterdata.CommandCurrency;
import com.ui.basic.ibatis.dao.masterdata.TblMCountryDAO;
import com.ui.basic.ibatis.dao.masterdata.TblMCurrenciesDAO;
import com.ui.basic.ibatis.model.masterdata.TblMCountryExample;
import com.ui.basic.ibatis.model.masterdata.TblMCurrencies;
import com.ui.basic.ibatis.model.masterdata.TblMCurrenciesExample;
import com.ui.basic.service.administrative.ExchangeRateService;

@Service("currencyService")
public class CurrencyServiceImpl implements CurrencyService {
	private static final transient org.apache.commons.logging.Log LOGGER = org.apache.commons.logging.LogFactory
			.getLog(CurrencyServiceImpl.class);

	@Autowired
	private TblMCurrenciesDAO tblMCurrenciesDAO;
	@Autowired
	private TblMCountryDAO tblMCountryDAO;
	@Autowired
	private ExchangeRateService exchangeRateService;


	public void setTblMCurrenciesDAO(TblMCurrenciesDAO tblMCurrenciesDAO) {
		this.tblMCurrenciesDAO = tblMCurrenciesDAO;
	}

	public void setTblMCountryDAO(TblMCountryDAO tblMCountryDAO) {
		this.tblMCountryDAO = tblMCountryDAO;
	}

	public CommandCurrency getCurrency(CommandCurrency commandCurrency)
			throws SQLException {
		LOGGER.debug(">>>CurrencyServiceImpl.getCurrency");
		if (commandCurrency != null) {
			TblMCurrenciesExample example = new TblMCurrenciesExample();

			int rec = 0;
			try {
				rec = tblMCurrenciesDAO.countByExample(example);
			} catch (NullPointerException npe) {
				LOGGER.error(npe);
			}
			commandCurrency.calculate(rec);
			example.setIntRowNum((commandCurrency.getPgIndex() - 1)
					* commandCurrency.getPgRows());
			example.setIntRowsPerPage(commandCurrency.getPgRows());
			example.setOrderByClause("currabrev");
			commandCurrency.setData(tblMCurrenciesDAO.selectByExample(example));
		}
		return commandCurrency;
	}

	public CommandCurrency getCurrencyDetail(CommandCurrency commandCurrency)
			throws SQLException {
		LOGGER.debug(">>>CurrencyServiceImpl.getCurrencyDetail");
		commandCurrency.setProperty(tblMCurrenciesDAO.selectByPrimaryKey(commandCurrency.getId()));
		return commandCurrency;
	}

	public void setCombo(CommandCurrency commandCurrency) throws SQLException {
		LOGGER.debug(">>>CurrencyServiceImpl.setCombo");
		TblMCountryExample countryExample = new TblMCountryExample();
		countryExample.setOrderByClause("CODE");
		commandCurrency.setCountryList(ComboBoxPopulator.populate(
				tblMCountryDAO.selectByExample(countryExample), "id",
				new String[] { "code", "name" }, "-"));
	}

	public void setComboSelective(CommandCurrency commandCurrency)
			throws SQLException {
		LOGGER.debug(">>>CurrencyServiceImpl.setComboSelective");
		
		List<TblMCurrencies> currList = tblMCurrenciesDAO.selectByExample(new TblMCurrenciesExample());
		List<Long> countryIdExist = new ArrayList<Long>();
		for (TblMCurrencies tblMCurrencies : currList) {
			countryIdExist.add(tblMCurrencies.getCountryId());
		}
		
		TblMCountryExample countryExample = new TblMCountryExample();
		countryExample.createCriteria().andIdNotIn(countryIdExist);
		countryExample.setOrderByClause("CODE");
		commandCurrency.setCountryList(ComboBoxPopulator.populate(
				tblMCountryDAO.selectByExample(countryExample), "id",
				new String[] { "code", "name" }, "-"));

	}

	public void save(CommandCurrency commandCurrency) throws SQLException {
		LOGGER.debug(">>>CurrencyServiceImpl.save");
		CurrencyAssembler assembler = new CurrencyAssembler();
		TblMCurrencies currency = assembler.toEntity(commandCurrency);

		if(commandCurrency.getId() != null){
			currency.setChangedby((String) (SecurityUtils.getSubject().getSession()
					.getAttribute(Globals.CURRENT_USER_NAME)));
			currency.setChangeddate(new Date());
			try {
				tblMCurrenciesDAO.updateByPrimaryKeySelective(currency);
			} catch (SQLException e) {
				LOGGER.error("***SQL Code == " + e.getErrorCode());
				LOGGER.error("***error msg == " + e.getMessage());
				throw e;
			}
		}else{
			currency.setCreatedby((String) (SecurityUtils.getSubject().getSession()
					.getAttribute(Globals.CURRENT_USER_NAME)));
			currency.setCreateddate(new Date());
			try {
				//tblMCurrenciesDAO.insert(currency);
				exchangeRateService.setupNewCurrency(currency);
			} catch (SQLException e) {
				LOGGER.error("***SQL Code == " + e.getErrorCode());
				LOGGER.error("***error msg == " + e.getMessage());
				throw e;
			}
		}
	}

	public void delete(CommandCurrency commandCurrency) throws SQLException {
		LOGGER.debug(">>>CurrencyServiceImpl.delete");
		TblMCurrenciesExample example = new TblMCurrenciesExample();
		
		if(commandCurrency.getId()!=null){
			example.createCriteria().andIdEqualTo(commandCurrency.getId());
		}else if(commandCurrency.getSelectedRecord()!=null && commandCurrency.getSelectedRecord().length>0){
			List<Long> idList = new ArrayList<Long>();
			for(String strId: commandCurrency.getSelectedRecord())
				idList.add(Long.valueOf(strId));
			example.createCriteria().andIdIn(idList);
		}
		
		tblMCurrenciesDAO.deleteByExample(example);		
	}

}
